/**
 * 2018年4月17日下午9:41:43
 */
package com.an.framework;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * 
 * @目的：redis配置类
 * 
 * @author 86171
 * @date 2018年4月17日下午9:41:43
 * 
 */
@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisConfig {
	
	@Value("${redis.host}")
	private String hostName;

	@Value("${redis.pass}")
	private String pass;

	@Value("${redis.port}")
	private int port;

	@Bean
	public static PropertySourcesPlaceholderConfigurer pspc() {
		return new PropertySourcesPlaceholderConfigurer();
	}

	/*@Bean
	RedisStandaloneConfiguration get() {
		RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
		configuration.setHostName(hostName);
		RedisPassword password = RedisPassword.of(pass);
		configuration.setPassword(password);
		configuration.setPort(port);
		return configuration;

	}
*/
	@Bean
	public JedisConnectionFactory connectionFactory( ) {
		JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
		connectionFactory.setHostName(hostName);
		connectionFactory.setPort(port);
		connectionFactory.setPassword(pass);
		return connectionFactory;
	}

	@Bean
	RedisMessageListenerContainer container(JedisConnectionFactory factory, MessageListenerAdapter listenerAdapter) {
		RedisMessageListenerContainer container = new RedisMessageListenerContainer();
		container.setConnectionFactory(factory);
		container.addMessageListener(listenerAdapter,new PatternTopic("chat"));
		return container;

	}

	@Bean
	Receiver getRe() {
		return new Receiver();
	}

	@Bean
	MessageListenerAdapter getMessageListenerAdapter(Receiver obj) {
		return new MessageListenerAdapter(obj);
	}
}
